﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class HYTakeOffFittingConnect : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			Result result;
			try
			{
				if (commandData == null)
				{
					throw new System.ArgumentNullException("commandData", "is null");
				}
				UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
				Document document = activeUIDocument.Document;
				Selection selection = activeUIDocument.Selection;
				IList<Duct> list = new List<Duct>();
				IList<XYZ> list2 = new List<XYZ>();
				ISelectionFilter selectionFilter = new DuctSelectionFilter();
				HYDuctConnect hyductConnect = new HYDuctConnect();
				ILayForm layForm = new ILayForm(document);
				FamilySymbol familySymbol = layForm.LayDuctFittingFormShow(document, "风管接头");
				if (familySymbol == null)
				{
					result = Autodesk.Revit.UI.Result.Cancelled;
				}
				else
				{
					string text = "侧连接";
					if (layForm.ductFittingFlag == 1)
					{
						activeUIDocument.PromptForFamilyInstancePlacement(familySymbol);
						result = Autodesk.Revit.UI.Result.Succeeded;
					}
					else
					{
						for (;;)
						{
							transaction = new Transaction(document, text);
							transaction.Start();
							list.Clear();
							list2.Clear();
							RevitVersionFuncs.ClearSelection(selection);
							Reference reference = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, selectionFilter, "选择第一个风管");
							list.Add(document.GetElement(reference) as Duct);
							list2.Add(reference.GlobalPoint);
							reference = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, selectionFilter, "选择第二个风管");
							list.Add(document.GetElement(reference) as Duct);
							list2.Add(reference.GlobalPoint);
							FamilyInstance familyInstance = hyductConnect.NewTakeOffFitting(list, list2, familySymbol);
							if (familyInstance == null)
							{
								MessageBox.Show("连接失败", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
							}
							else
							{
								familyInstance.Symbol = familySymbol;
								document.Regenerate();
							}
							transaction.Commit();
						}
					}
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null)
				{
					TransactionStatus status = transaction.GetStatus();
					if ((int)status == 1)
					{
						transaction.Commit();
					}
					else if ((int)status == 5)
					{
						transaction.RollBack();
					}
				}
			}
			return result;
		}
	}
}
